<?php
require_once('security.inc');

function header_common($title, $style=null, $page = null) {
	header('Content-type: text/html; charset=iso-8859-1');
	select_language();

	?>
	<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">	
	<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<title><?php echo (defined($title)?constant($title):$title)?></title>
		<link rel="stylesheet" type="text/css" href="<?php echo CSS_PATH . SCRIPT_OLOGISTICS_CSS; ?>"  media="all"/>
		
		<!--[if lte IE 6]>
			<link rel="stylesheet" media="all" type="text/css" href="<?php echo CSS_PATH;?>dropdown_ie.css" />
		<![endif]-->
		<?php
			if ($style != null) {
				echo '<link rel="stylesheet" type="text/css" href="'.CSS_PATH.$style.'.css"  media="all"/>';
			}
			select_js_language();      
		?>
		<script type="text/javascript" src="<?php echo JAVASCRIPT_PATH . SCRIPT_OLOGISTICS_JS; ?>"></script>
	</head>	
	<?php
}

function html_header($title, $style=null, $categorie = null, $bara_visible = null, $page = null) {
	header_common($title, $style, $page);
	
	?>
	<body>	

	<div id="logo" class="pngfix">
		<img height="76" width="200" src="images/site_images/logo_program.png" />
	</div>
	  
	<?php menu_bar($categorie, $bara_visible); ?>
	<div id="main">
		
	<?php    
}

function html_header_login($title, $style=null) {
	header_common($title, $style);
	
	?>
	<body>
		
	<div id="main"> 
		
	<?php
}

function __autoload($className) {
    if ($className == "Mail") {		
    	require_once('./include/classes/phpmailer/class.mail.php');    			       
    }
    else if ($className == "PHPMailer") {		
    	require_once('./include/classes/phpmailer/class.phpmailer.php');    			       
    }    
    else if (strpos($className, 'Exception') === false && $className != "FPDF" && $className != 'Mail' && $className != 'PHPMailer') {        
		if ($className == 'Adress') $className = 'Address';
		require_once('./include/classes/' . $className . '.class.php');
	}
	else {
        if ($className != 'Mail' && $className != 'PHPMailer')
		    require_once('./include/classes/exceptions/' . $className . '.class.php');
	}		
}

function show_buton_icon($titlu, $id, $functieJS, $class = 'buton_bara', $image_name, $tooltip = "") {
	?>
    <span class="pngfix">
        <button title="<?php echo $tooltip; ?>" class="<?php echo $class; ?>" id="<?php echo $id; ?>" onclick="javascript: <?php echo $functieJS; ?>">
		    <span class=""><img src="<?php echo IMG_PATH . $image_name; ?>" /></span><span class="buton_bara_with_icon"><?php echo $titlu; ?></span>
	    </button>
    </span>
    <?php
}

function show_buton($titlu, $id, $functieJS, $class = 'buton_bara') {
    ?>
    <span class="pngfix">
	    <button class="<?php echo $class; ?>" id="<?php echo $id; ?>" onclick="javascript: <?php echo $functieJS; ?>">
			<span class="buton_bara_with_icon2"><?php echo $titlu; ?></span>
		</button>
	</span>
    <?php
}

function show_standard_buttons() {
	if (has_permission(CT_PERM_FIRME)) {
        draw_add_firma_button();
		show_buton(LANG_View_Firms, 'view_firms_buton', "view_firms_buton()");
	}
	if (has_permission(CT_PERM_COMENZI)) {
	    draw_comanda_add_button();
		show_buton(LANG_Show_comenzi, 'show_comands_buton', "show_comenzi_buton();");
	}
	if (has_permission(CT_PERM_PLATI)) {
		show_buton(LANG_View_Plata, 'platilist', "plati_list()");
		show_buton(LANG_View_Incasare, 'incasarilist', "incasari_list()");
	}
}

function show_firme_bar() {
	draw_add_firma_button();
	draw_edit_firma_button();
	show_buton_icon("", 'view_firma_buton', "view_firma_buton()", "buton_bara", "view_icon_btn.png", LANG_View_Firma);
	draw_print_firma_button();
	draw_delete_firma_button();
    draw_contact_search_button();
	if (has_permission(CT_PERM_PLATI)) {
		show_buton(LANG_View_Plata, 'platilist', "plati_list()");
		show_buton(LANG_View_Incasare, 'incasarilist', "incasari_list()");
	}
}

function show_statistici() {	
	show_buton(LANG_Back_to_home, 'back_to_home', "back_to_home()");
	if (has_permission(CT_PERM_PLATI)) {
		show_buton(LANG_View_Plata, 'platilist', "plati_list()");
		show_buton(LANG_View_Incasare, 'incasarilist', "incasari_list()");
	}
	if (has_permission(CT_PERM_FIRME)) {
		show_buton(LANG_View_Firms, 'view_firms_buton', "view_firms_buton()");
	}
	if (has_permission(CT_PERM_COMENZI)) {
		show_buton(LANG_Show_comenzi, 'show_comands_buton', "show_comenzi_buton();");
	}
}

function show_dashboard() {
	show_standard_buttons();
}

function show_home_bar() {
	show_standard_buttons();
}

function cauta_contacte_bar() {
	show_standard_buttons();
}

function view_plata_bar() {
	if (has_permission(CT_PERM_FIRME)) {
        draw_add_firma_button();
		show_buton(LANG_View_Firms, 'view_firms_buton', "view_firms_buton()");
	}
	if (has_permission(CT_PERM_COMENZI)) {
        draw_comanda_add_button();
		show_buton(LANG_View_Comanda2, 'view_comanda_button', "view_comanda()");
	}
	show_buton(LANG_View_Incasare, 'incasarilist', "incasari_list()");
}

function view_incasare_bar() {
	if (has_permission(CT_PERM_FIRME)) {
        draw_add_firma_button();
		show_buton(LANG_View_Firms, 'view_firms_buton', "view_firms_buton()");
	}
	if (has_permission(CT_PERM_COMENZI)) {
        draw_comanda_add_button();
		show_buton(LANG_View_Comanda2, 'view_comanda_button', "view_comanda()");
	}
	show_buton(LANG_View_Plata, 'view_incasari_button', "viewplati()");
}

function opt_generale_bar() {
	show_standard_buttons();
}

function user_profile_bar() {
	show_standard_buttons();
}

function add_plata_bar() {
	if (has_permission(CT_PERM_FIRME)) {
		show_buton(LANG_View_Firms, 'view_firms_buton', "view_firms_buton()");
	}
	if (has_permission(CT_PERM_COMENZI)) {
		show_buton(LANG_Show_comenzi, 'show_comands_buton', "show_comenzi_buton()");
	}
	show_buton(LANG_View_Plata, 'platilist', "plati_list()");
	show_buton(LANG_View_Incasare, 'incasarilist', "incasari_list()");
}

function add_client_bar() {
	show_standard_buttons();
}

function change_pass_bar() {
	show_standard_buttons();
}

function contact_bar() {
    show_standard_buttons();
}

function stari_plati_bar() {
    show_standard_buttons();
}

function show_comenzi_bar() {
	draw_edit_comanda_button();
	show_buton_icon("", 'view_comanda_buton', "view_comanda_buton()", "buton_bara", "view_icon_btn.png", LANG_View_Comanda);
	draw_print_comanda_button();
	if (has_permission(CT_PERM_PLATI)) {
		show_buton(LANG_Plata_Comanda, 'plata_comanda_buton', "plata_comanda_buton()");
		show_buton(LANG_Incasare_Comanda, 'incasare_comanda_buton', "incasare_comanda_buton()");
        show_buton(LANG_Plata_Incasare, 'plata_incasare_comanda_buton', "plata_incasare_comanda_buton()");
	}
	show_buton_icon("", 'sterge_comanda_buton', "sterge_comanda_buton()", "buton_bara", "delete_icon_btn.png", LANG_DELETE);
	if (has_permission(CT_PERM_PLATI)) {
		show_buton(LANG_View_Plata, 'view_plata_button', "viewplati()");
		show_buton(LANG_View_Incasare, 'view_incasari_button', "view_incasari()");
	}
    show_buton_icon("", 'duplicare_comanda_button', "duplicare_comanda_button()", "buton_bara", "duplicare_icon_btn.png", LANG_Duplicare);
	show_buton(LANG_Facturare, 'print_factura', "print_factura_button()");
}

function view_comanda_bar() {
    draw_comanda_add_button();
    draw_edit_comanda_button();
    draw_print_comanda_button();

	if (has_permission(CT_PERM_PLATI)) {
		show_buton(LANG_Plata_Comanda, 'plata_comanda_buton', "plata_comanda_from_vizualizare_button()");
		show_buton(LANG_Incasare_Comanda, 'incasare_comanda_buton', "incasare_comanda_from_vizualizare_button()");
	}
    draw_delete_comanda_button();
	if (has_permission(CT_PERM_PLATI)) {
		show_buton(LANG_View_Plata, 'platilist', "plati_list()");	
		show_buton(LANG_View_Incasare, 'incasarilist', "incasari_list()");	
	}
}

function add_comanda_bar() {	
	if (has_permission(CT_PERM_FIRME)) {
        draw_add_firma_button();
		show_buton(LANG_View_Firms, 'view_firms_buton', "view_firms_buton()");
	}
	if (has_permission(CT_PERM_PLATI)) {
		show_buton(LANG_View_Plata, 'platilist', "plati_list()");	
		show_buton(LANG_View_Incasare, 'incasarilist', "incasari_list()");		
	}
}

function add_comanda_edit_bar() {
	if (has_permission(CT_PERM_FIRME)) {
		draw_add_firma_button();
		show_buton(LANG_View_Firms, 'view_firms_buton', "view_firms_buton()");
	}
	if (has_permission(CT_PERM_PLATI)) {
		show_buton(LANG_View_Plata, 'platilist', "plati_list()");	
		show_buton(LANG_View_Incasare, 'incasarilist', "incasari_list()");		
	}
	show_buton(LANG_Print_Comanda, 'print_comanda_buton', "print_comanda_buton()");	
}

function view_firma_bar() {
    draw_add_firma_button();
    draw_edit_firma_button();
	show_buton(LANG_View_Firms, 'view_firms_buton', "view_firms_buton()");
    draw_print_firma_button();
    draw_delete_firma_button(); 
}

function add_firme_bar() {
    draw_contact_search_button();
	if (has_permission(CT_PERM_COMENZI)) {
		show_buton(LANG_Show_comenzi, 'show_comands_buton', "show_comenzi_buton();");
        draw_comanda_add_button();
	}
	if (has_permission(CT_PERM_PLATI)) {
		show_buton(LANG_View_Plata, 'platilist', "plati_list()");	
		show_buton(LANG_View_Incasare, 'incasarilist', "incasari_list()");	
	}
}

function signal_error_bar() {
	show_standard_buttons();	
}

function add_users_bar() {
    show_standard_buttons();
}

function servicii_bar() {
	show_standard_buttons();
}

function draw_delete_firma_button() {
    show_buton_icon("", 'sterge_firma_buton', "sterge_firma_buton()", "buton_bara", "delete_icon_btn.png", LANG_DELETE);
}

function draw_print_firma_button() {
    show_buton_icon("", 'print_firma_buton', "print_firma_buton()", "buton_bara", "print_icon_btn.png", LANG_PRINT_FIRMA);
}

function draw_edit_firma_button() {
    show_buton_icon("", 'edit_firma_buton', "edit_firma_buton()", "buton_bara", "edit_icon_btn.png", LANG_Edit_Firma);
}

function draw_print_comanda_button() {
    show_buton_icon("", 'print_comanda_buton', "print_comanda_buton()", "buton_bara", "print_icon_btn.png", LANG_Print_Comanda);
}

function draw_add_firma_button() {
    show_buton_icon(LANG_Firma, 'add_firma_buton', "add_firma_buton()", "buton_bara", "add_icon_btn.png", LANG_ADD_FIRMA);
}

function draw_contact_search_button() {
    show_buton_icon(LANG_Contact_icon_button, 'cauta_contact_buton', "cauta_contact_buton()", "buton_bara", "find_icon_btn.png", LANG_Cauta_Contact);
}

function draw_comanda_add_button() {
    show_buton_icon(LANG_Comanda, 'add_comanda_buton', "add_comanda_buton()", "buton_bara", "add_icon_btn.png", LANG_Add_Comanda);
}

function draw_delete_comanda_button() {
    show_buton_icon("", 'sterge_comanda_buton', "sterge_comanda_buton_view()", "buton_bara", "delete_icon_btn.png", LANG_DELETE); 
}

function draw_edit_comanda_button() {
    show_buton_icon("", 'edit_comanda_buton', "edit_comanda_buton()", "buton_bara", "edit_icon_btn.png", LANG_Edit_Comanda);
}

function menu_bar($categorie = null, $vizibil = null) {
	
	if ($vizibil != "no") {
		?>
			<div id="bar">	
				<?php						
				
					if ($categorie == "servicii") {
						servicii_bar();
					}					
					if ($categorie == "add_users") {
						add_users_bar();
					}										
					if ($categorie == "signal_error") {
						signal_error_bar();
					}										
					if ($categorie == "show_firms") {
						show_firme_bar();
					}						
					if ($categorie == "statistici") {
						show_statistici();
					}						
					if ($categorie == "home") {
						show_home_bar();
					}	
					if ($categorie == 'dashboard') {
						show_dashboard();
					}
					if ($categorie == "view_firma") {
						view_firma_bar();
					}			
					if ($categorie == "add_firma") {
						add_firme_bar();
					}
					if ($categorie == "show_comenzi") {
						show_comenzi_bar();
					}
					if ($categorie == "view_comanda") {
						view_comanda_bar();
					}	
					if ($categorie == "add_comanda") {
						add_comanda_bar();
					}		
					if ($categorie == "add_comanda_edit") {
						add_comanda_edit_bar();						
					}			
					if ($categorie == "cauta_contacte") {
						cauta_contacte_bar();
					}
					if ($categorie == "view_plata") {
						view_plata_bar();
					}					
					if ($categorie == "view_incasare") {
						view_incasare_bar();
					}				
					if ($categorie == "opt_generale") {
						opt_generale_bar();
					}			
					if ($categorie == "user_profile") {
						user_profile_bar();
					}	
					if ($categorie == "add_plata") {
						add_plata_bar();
					}												
					if ($categorie == "change_pass") {
						change_pass_bar();
					}		
					if ($categorie == "contact") {
						contact_bar();
					}					
					if ($categorie == "add_plati") {
						add_plata_bar();
					}
                    if ($categorie == "add_client") {
                        add_client_bar();
                    }    
                    if ($categorie == "stari_plati") stari_plati_bar();
				?>	
			</div>
		<?php
	}
}

function html_footer($page = null) {
	?>	
			</div>
			<?php
				if (empty($page)) {
					echo '<div id="header" class="menu">';
						menu();
					echo '</div>'; 
				}
			?>	
		</body>
	</html>
<?php
flush();
}

function include_js($script) {
	echo '<script type="text/javascript" language="javascript" src="'.JAVASCRIPT_PATH.$script.'"></script>' . "\n";
}

function include_css($css) {
	echo '<link rel="stylesheet" href="' . CSS_PATH . $css . '" media="screen" type="text/css">' . "\n";
}

function select_language(){	
	$language = 'romana';
	$lang_file = LANG_PATH.'lang_'.$language.'.inc';
	
	if (isset($_SESSION['language']))
		$language = $_SESSION['language'];
	if (is_readable(LANG_PATH.'lang_'.$language.'.inc'))
		$lang_file = LANG_PATH.'lang_'.$language.'.inc';

	require_once($lang_file);
	return $lang_file;
}

function select_js_language(){
	$language = 'romana';
	$lang_file = LANG_PATH.'lang_'.$language.'.js';
	
	if (isset($_SESSION['language']))
	    $language = $_SESSION['language'];
	if (is_readable(LANG_PATH.'lang_'.$language.'.js'))
	    $lang_file = LANG_PATH.'lang_'.$language.'.js';
	echo "<script type=\"text/javascript\" src=\"".$lang_file."\" defer=\"defer\"></script>\n";
}

//outputs a special error message, if set
function output_error_msg() {
	if (isset($_SESSION["error_msg"])) {
		echo '<div class="error">';
			echo '<span class="pngfix">&nbsp;<img src="images/atentie.png" /> <span style="vertical-align: 80%;">&nbsp;';
			if (substr($_SESSION["error_msg"],0,5) == 'LANG_' && defined($_SESSION["error_msg"]))
		  		echo constant($_SESSION["error_msg"])."</span></span></div>";
			else 
		  		echo $_SESSION["error_msg"] . "</span></span></div>";
		unset($_SESSION["error_msg"]);
	}	
}

//outputs a special notice message, if set
function output_notice_msg() {
	if (isset($_SESSION["notice_msg"]) && !empty($_SESSION["notice_msg"])) {
	    echo '<div class="notice" id="notice_msg">';
		    if (strtoupper(substr($_SESSION["notice_msg"],0,5)) == 'LANG_' && defined($_SESSION["notice_msg"])) {
		    	echo '<span class="pngfix">&nbsp;<img src="images/info.png" /> <span style="vertical-align: 80%;">&nbsp;' . constant($_SESSION["notice_msg"]) . '</span></span>';
		    	if ($_SESSION["notice_msg"] == 'LANG_va_apropiati_de_depasire' ||
		    		$_SESSION["notice_msg"] == 'LANG_dupa_15_zile_inchide_robinetul') {
		    		// mai adaug un link in avertizare doar daca are tip...de platire factura		    		
		    		if ($_SESSION['user_logat']->tip | CT_PERM_ADMIN_ABONAMENT) {
		    			echo '&nbsp;' . LANG_pentru_plata_accesati . " <a href=\"stari_plati.php\">" . LANG_here . "</a>";		
		    		}
		    	}
		    }
		    else echo '<span class="pngfix">&nbsp;<img src="images/info.png" /> <span style="vertical-align: 80%;">&nbsp;' . $_SESSION["notice_msg"] . "</span></span>"; 	    
		    if ($_SESSION["notice_msg"] != 'LANG_va_apropiati_de_depasire' && $_SESSION["notice_msg"] != 'LANG_dupa_15_zile_inchide_robinetul')
		    	unset($_SESSION["notice_msg"]);
	    echo "</div>";    
	}
}

function form_language_field($selected) {
  echo '<select class="box" name="formLanguage">';
  global $languages;
  foreach ($languages as $language)
    if ($language === $selected)
      echo "<option selected=selected>$language</option>";
    else
      echo "<option>$language</option>";
  echo '</select>';
}

function form_currency_field($selected) {
  echo '<select class="box" name="currency">';
  global $currencies;
  if (!isset($selected)) $selected = 0;
  $i = 0;
  foreach ($currencies as $currency) {
    if ($i == $selected)
      echo "<option value=$i selected=selected>$currency</option>";
    else
      echo "<option value=$i>$currency</option>";
    $i++;
  }
  echo '</select>';
}

function currencyOutput($sum, $curr) {
  global $currency_symbol;
  return number_format($sum, 2, CT_CURRENCY_DEC_SEP, CT_CURRENCY_TH_SEP).$currency_symbol[$curr];
}

function rows($row) {
	if ($row %2) return "<tr class=\"oddrow\">\n\t";
	else return "<tr class=\"evenrow\">\n\t";
}

function menu() {
	?>
		<ul>
			<li><a class="hide"><?php echo LANG_Program; ?></a>
				<!--[if lte IE 6]><a href="#"><?php echo LANG_Program; ?><table><tr><td><![endif]-->
				<ul>
					<li><a href="home.php"><?php echo LANG_Home; ?></a></li>
					<li><a href="dashboard.php"><?php echo LANG_Dashboard; ?></a></li>
					<li><a href="logout.php"><?php echo LANG_Iesire; ?></a></li>				
				</ul>			
				<!--[if lte IE 6]></td></tr></table></a><![endif]-->		
			</li>	
		
			<li><a class="hide"><?php echo LANG_Firme; ?></a>		
				<!--[if lte IE 6]><a href="#"><?php echo LANG_Firme; ?><table><tr><td><![endif]-->
				<ul>
					<li><a href="add_firma.php"><?php echo LANG_Add_Firm; ?></a></li>
					<li><a href="show_firms.php"><?php echo LANG_Show_Firms; ?></a></li>	
					<li><a href="cauta_contacte.php"><?php echo LANG_Cauta_Contacte; ?></a></li>	
				</ul>			
				<!--[if lte IE 6]></td></tr></table></a><![endif]-->		
			</li>
		
			<li><a class="hide"><?php echo LANG_Comenzi; ?></a>		
				<!--[if lte IE 6]><a href="#"><?php echo LANG_Comenzi; ?><table><tr><td><![endif]-->		
				<ul>
					<li><a href="add_comanda.php"><?php echo LANG_Add_Comanda; ?></a></li>
					<li><a href="show_comenzi.php"><?php echo LANG_Show_comenzi_menu; ?></a></li>	
				</ul>		
				<!--[if lte IE 6]></td></tr></table></a><![endif]-->		
			</li>
			<?php if (has_permission(CT_PERM_PLATI)) { ?>
			<li><a class="hide"><?php echo LANG_Plati_Incasari; ?></a>		
				<!--[if lte IE 6]><a href="#"><?php echo LANG_Plati_Incasari; ?><table><tr><td><![endif]-->
				<ul>
					<!--<li><a href="spies.html"><?php echo LANG_Add_plata; ?></a></li>
					<li><a href="spies.html"><?php echo LANG_Add_Incasari; ?></a></li>-->
					<li><a href="view_plata.php"><?php echo LANG_Show_plati; ?></a></li>	
					<li><a href="view_incasare.php"><?php echo LANG_Show_incasari; ?></a></li>	
				</ul>		
				<!--[if lte IE 6]></td></tr></table></a><![endif]-->		
			</li>
			<?php } ?>
		
			<li><a class="hide"><?php echo LANG_Optiuni; ?></a>		
				<!--[if lte IE 6]><a href="#"><?php echo LANG_Optiuni; ?><table><tr><td><![endif]-->		
				<ul>
					<li><a href="user_profile.php"><?php echo LANG_Profile; ?></a></li>
					<li><a href="change_password.php"><?php echo LANG_Change_password; ?></a></li>
					
					<?php if (has_permission(CT_PERM_EDIT_CLIENT)) { ?>
					<li><a href="opt_generale.php"><?php echo LANG_Optiuni_Generale; ?></a></li>
					<?php } ?>
					
					<?php if (has_permission(CT_PERM_EDIT_CLIENT)) { ?>
	      				<li><a href="add_client.php"><?php echo LANG_Client; ?></a></li>
	      			<?php } ?>
	      			<?php if (has_permission(CT_PERM_EDIT_USERS)) { ?>
	      				<li><a href="add_users.php"><?php echo LANG_Administrare_Useri; ?></a></li>
	      			<?php } ?>
                    <?php if (has_permission(CT_PERM_ADMIN_ABONAMENT)) { ?>
                          <li><a href="stari_plati.php"><?php echo LANG_Stare_Plati; ?></a></li>
                    <?php } ?>                    
				</ul>		
				<!--[if lte IE 6]></td></tr></table></a><![endif]-->		
			</li>
			<?php
				if (has_permission(CT_PERM_STATISTICI)) { ?>
					<li><a href="stats.php?criteriu=profit&perioada=luna_curenta&tip=Line"><?php echo LANG_Statistici_comenzi; ?></a></li>
				<?php 
				} 
				?>
			<li><a class="hide"><!-- <img align="bottom" src="images/help_menu_icon.png" border="0" /> --><?php echo LANG_Help; ?></a>
				<!--[if lte IE 6]><a href="#"><?php echo LANG_Contact; ?><table><tr><td><![endif]-->		
				<ul>
					<li><a href="contact.php"><?php echo LANG_Contact; ?></a></li>
					<li><a href="servicii.php"><?php echo LANG_Servicii; ?></a></li>
					<li><a href="help.php"><?php echo LANG_Help; ?></a></li>
				</ul>		
				<!--[if lte IE 6]></td></tr></table></a><![endif]-->		
			</li>					
		</ul>
	<?php
}

?>
